﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;
using System.Data;
using System.IO;

namespace TranTekBE.Marketing
{
    public partial class Add_Position_Stores : PagePopUp
    {
        public string path_image = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["store"] == null)
            {
                LoadData("");
            }
            else
            {
                LoadData(Request.QueryString["store"]);
            }
            if (!IsPostBack)
            {
                FunctionsUtils.GetDropListStore(droplist_CH);
                FunctionsUtils.GetDropListStore(droplist_CH_Loc);
                droplist_CH_Loc.SelectedValue = Request.QueryString["store"];
            }
        }

        void LoadData(string getstore)
        {
            string sql_loc = @"SELECT * FROM ViTriCuaHang as A 
            Left Join HinhViTri as B On A.ViTriID = B.ViTriID 
            Where 1=1
            ";
            string sql_store = "";

            if (getstore != "")
            {
                sql_store = " AND CuaHang = '" + getstore + "'";
            }

            sql_loc = sql_loc + sql_store;

            DataSet ds = FunctionsUtils.Database2.ExecuteQuery(sql_loc);

            DataTable dt = ds.Tables[0];

            PagedDataSource pgitems = new PagedDataSource();

            System.Data.DataView dv = new System.Data.DataView(dt);

            pgitems.DataSource = dv;

            pgitems.AllowPaging = true;

            pgitems.PageSize = 10;

            pgitems.CurrentPageIndex = PageNumber;

            if (pgitems.PageCount > 1)
            {

                rptPages.Visible = true;

                System.Collections.ArrayList pages = new System.Collections.ArrayList();

                for (int i = 0; i < pgitems.PageCount; i++)

                    pages.Add((i + 1).ToString());

                rptPages.DataSource = pages;

                rptPages.DataBind();

            }

            else

                rptPages.Visible = false;

            rpEvent.DataSource = pgitems;

            rpEvent.DataBind();

        }

        public int PageNumber
        {

            get
            {

                if (ViewState["PageNumber"] != null)

                    return Convert.ToInt32(ViewState["PageNumber"]);

                else

                    return 0;

            }

            set
            {

                ViewState["PageNumber"] = value;

            }

        }

        protected void rptPages_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

            PageNumber = Convert.ToInt32(e.CommandArgument) - 1;

            if (Request.QueryString["store"] == null)
            {
                LoadData("");
            }
            else
            {
                LoadData(Request.QueryString["store"]);
            }

        }

        bool CheckFileType(string fileName)
        {

            string ext = Path.GetExtension(fileName);

            switch (ext.ToLower())
            {

                case ".gif":

                    return true;

                case ".png":

                    return true;

                case ".jpg":

                    return true;

                case ".jpeg":

                    return true;

                default:

                    return false;

            }

        }
        protected void btnAddEvent_Click(object sender, EventArgs e)
        {
            try
            {
                // Image
                if (FileUpload_Image.HasFile)
                {
                    if (CheckFileType(FileUpload_Image.FileName))
                    {
                        string date = DateTime.Now.ToString("yyMMddHHmmss");
                        string fileName = date + Path.GetFileName(FileUpload_Image.PostedFile.FileName);
                        path_image = "/images/Marketing/ViTriCH/" + fileName;

                        FileUpload_Image.PostedFile.SaveAs(Server.MapPath(path_image));
                    }
                    else
                    {
                        ltrErr.Text = "File upload không đúng!";
                    }
                }
                // Insert
                if (txtViTriCH.Text == "" && txtViTriCH.Text == null && droplist_CH.SelectedIndex == 0)
                {
                    ltrErr.Text = "Lỗi! Vui lòng kiểm tra lại";
                    ltrErr.ForeColor = System.Drawing.Color.Red;
                }
                else
                {
                    string vitriCH = txtViTriCH.Text.ToString().Replace("'", "''");
                    string cuahang = droplist_CH.SelectedValue.ToString();
                    string url_image = path_image;

                    try
                    {
                        // Insert 
                        string sql_add = @"
                    insert into ViTriCuaHang (TenViTri,CuaHang) values(N'" + vitriCH + "','" + cuahang + @"');
                    insert into HinhViTri (Url,VitriId) values(N'" + url_image + @"', SCOPE_IDENTITY())
                                    
                    ";

                        FunctionsUtils.Database2.ExecuteNonQuery(sql_add);
                        ltrErr.Text = "Thêm Thành Công!";
                        ltrErr.ForeColor = System.Drawing.Color.Red;
                    }
                    catch (Exception ex)
                    {
                        ltrErr.Text = ex.Message;
                        ltrErr.ForeColor = System.Drawing.Color.Red;
                    }
                    Response.Redirect(Request.RawUrl);
                }
            }
            catch (Exception err)
            {
                ltrErr.Text = err.Message;
            }

        }

        protected void droplist_CH_Loc_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("Add-Position-Stores.aspx?store=" + droplist_CH_Loc.SelectedValue.ToString());
        }

        

    }
}